using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace buildingLib.Prepare
{
    [Serializable()]
    public class FloorItem : BusinessBase<FloorItem>
    {
        #region Business Methods
        Guid m_id;
        string m_name = string.Empty;
        Guid m_buildingid;
        double m_buildingArea;
        string m_memo = string.Empty;

        public Guid ID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_id;
            }
        }

        public string Name
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_name;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_name != value)
                {
                    m_name = value;
                    PropertyHasChanged();
                }
            }
        }
        public Guid Buildingid
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_buildingid;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_buildingid != value)
                {
                    m_buildingid = value;
                    PropertyHasChanged();
                }
            }
        }
        public double BuildingArea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_buildingArea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_buildingArea != value)
                {
                    m_buildingArea = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Memo
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_memo;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_memo != value)
                {
                    m_memo = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return m_id;
        }
        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(null, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("", "");
        }

        #endregion

        #region Factory Methods

        internal static FloorItem NewFloorItem(Guid buildingID)
        {
            // TODO: change to use new keyword if not loading defaults
            return new FloorItem(buildingID);
        }

        internal static FloorItem GetFloorItem(SafeDataReader dr)
        {
            return new FloorItem(dr);
        }

        private FloorItem(Guid buildingID)
        {
            m_id = Guid.NewGuid();
            m_buildingid = buildingID;
            MarkAsChild();
        }

        private FloorItem(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            m_id = dr.GetGuid("id");

            m_name = dr.GetString("name");
            m_buildingid = dr.GetGuid("buildingid");
            m_buildingArea = dr.GetDouble("buildingArea");
            m_memo = dr.GetString("memo");
            MarkOld();
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AInsertFloorItem");
            
            DoInsertUpdate(dbcom);
            
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AUpdateFloorItem");
            

            DoInsertUpdate(dbcom);
            MarkOld();
        }

        private void DoInsertUpdate(DbCommand dbcom)
        {
            Database db = SQLDatabase.GetDatabase();
            db.AddInParameter(dbcom, "id", DbType.Guid, m_id);
            db.AddInParameter(dbcom, "name", DbType.String, m_name);
            db.AddInParameter(dbcom, "buildingid", DbType.Guid, m_buildingid);
            db.AddInParameter(dbcom, "buildingArea", DbType.Double, m_buildingArea);
            db.AddInParameter(dbcom, "memo", DbType.String, m_memo);

            db.ExecuteNonQuery(dbcom);
        }

        internal void DeleteSelf()
        {
            // if we're not dirty then don't update the database
            if (!this.IsDirty) return;

            // if we're new then don't update the database
            if (this.IsNew) return;

            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_ADeleteFloorItem");
            db.AddInParameter(dbcom, "id", DbType.Guid, m_id);

            db.ExecuteNonQuery(dbcom);

            MarkNew();
        }

        #endregion
    }//end of class	
}//end of namespace 

